package org.bee.service;

import jakarta.annotation.Resource;
import org.bee.enums.IsDelete;
import org.bee.input.RoleSearchVo;
import org.bee.mappers.RoleMapper;
import org.bee.model.Role;
import org.bee.model.RoleExample;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 角色服务
 */
@Service
public class RoleService {

    @Resource
    private RoleMapper roleMapper;

    public List<Role> selectAllRole(){
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        criteria.andIsDeletedEqualTo(IsDelete.FALSE.getCode());
        return roleMapper.selectByExample(roleExample);
    }

    /**
     * 查询角色表
     * @param roleSearchVo 角色表查询参数
     * @return 角色表列表
     */
    public List<Role> select(RoleSearchVo roleSearchVo) {
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (roleSearchVo.getRoleCode() != null) {
            criteria.andRoleCodeLike("%"+roleSearchVo.getRoleCode()+"%");
        }
        if (roleSearchVo.getRoleName() != null) {
            criteria.andRoleNameLike("%"+roleSearchVo.getRoleName()+"%");
        }
        criteria.andIsDeletedEqualTo(IsDelete.FALSE.getCode());
        return roleMapper.selectByExample(roleExample);
    }

    /**
     * 根据条件查询角色表
     * @param roleExample 条件
     * @return 角色列表
     */
    public List<Role> selectByExample(RoleExample roleExample) {
        return roleMapper.selectByExample(roleExample);
    }

    /**
     * 新增角色
     * @param role 角色
     * @return 新增结果
     */
    public int insert(Role role) {
        return roleMapper.insert(role);
    }

    /**
     * 更新主键更新角色
     * @param role  角色
     * @return 更新结果
     */
    public int update(Role role) {
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 删除角色
     * @param roleId 角色主键
     * @param isDelete 是否删除
     * @return 删除结果
     */
    public int delete(String roleId,Boolean isDelete) {
        if(isDelete){
            return roleMapper.deleteByPrimaryKey(roleId);
        }else{
            Role role = new Role();
            role.setRoleId(roleId);
            role.setIsDeleted(true);
            return roleMapper.updateByPrimaryKeySelective(role);
        }
    }
}
